package itqf.controller;



import itqf.anno.ContentType;
import itqf.entity.Cart;
import itqf.entity.Users;
import itqf.service.CartServiceImpl;
import itqf.service.impl.CartService;
import itqf.utils.SysConstant;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${PACKAGE_NAME}
 * 2020/9/16
 */
@WebServlet("/cartController")
public class cartController extends BaseController{
    private CartService cartService = new CartServiceImpl();
    public String addCart(HttpServletRequest request, HttpServletResponse response){
        Users user = (Users) request.getSession().getAttribute("user");
        if(user==null){
            return SysConstant.REDIRECT+SysConstant.FALG+"login.jsp";

        }
            //1.购物车中已有该产品 数量加1
            Cart cart = new Cart();
            int pid = Integer.parseInt(request.getParameter("goodsId"));
            BigDecimal price = new BigDecimal(request.getParameter("price"));
            cart.setUid(user.getId());
            cart.setPid(pid);
            cart.setCcount(price);
            cart.setCnum(1);
            //1.购物车中没有该产品 新增记录
        int i = cartService.addCart(cart);
        if(i>0){
            //添加成功
            return SysConstant.REDIRECT+SysConstant.FALG+"cartSuccess.jsp";
        }else{
            request.setAttribute("msg","添加失败");
            return SysConstant.FORWARD+SysConstant.FALG+"message.jsp";
        }
    }

    public String getCart(HttpServletRequest request, HttpServletResponse response){
        Users user = (Users) request.getSession().getAttribute("user");
        if(user==null){
            return SysConstant.REDIRECT+SysConstant.FALG+"login.jsp";
        }
        List<Cart> list = cartService.findAllCart(user.getId());
        request.setAttribute("carts",list);
        return SysConstant.FORWARD+SysConstant.FALG+"cart.jsp";
    }

    @ContentType("application/json;charset=utf-8")
    public Map<String,Object> mupdateCartNum(HttpServletRequest request, HttpServletResponse response){
        Users user = (Users) request.getSession().getAttribute("user");
        int pid = Integer.parseInt(request.getParameter("pid"));
        BigDecimal bigDecimal = new BigDecimal(request.getParameter("price"));
        int uid = user.getId();
        int i = cartService.mupdateNum(pid,uid,bigDecimal);
        Map<String,Object> map = new HashMap<>();
        if(i>0){
            map.put("flag",1);
        }else{
            map.put("flag",0);
        }
        return map;
    }
    @ContentType("application/json;charset=utf-8")
    public Map<String,Object> pupdateCartNum(HttpServletRequest request, HttpServletResponse response){
        Users user = (Users) request.getSession().getAttribute("user");
        int pid = Integer.parseInt(request.getParameter("pid"));
        BigDecimal bigDecimal = new BigDecimal(request.getParameter("price"));
        int uid = user.getId();
        int i = cartService.pupdateNum(pid,uid,bigDecimal);
        Map<String,Object> map = new HashMap<>();
        if(i>0){
            map.put("flag",1);
        }else{
            map.put("flag",0);
        }
        return map;
    }

    public String clearCart(HttpServletRequest request, HttpServletResponse response){
        Users user = (Users)request.getSession().getAttribute("user");
        int uid = user.getId();
        int pid = Integer.parseInt(request.getParameter("pid"));
        int count = cartService.delCart(uid, pid);
        if(count>0){
            return getCart(request,response);
        }else{
            request.setAttribute("msg","添加失败");
            return SysConstant.FORWARD+SysConstant.FALG+"message.jsp";
        }
    }
    @ContentType("application/json;charset=utf-8")
    public Map<String,Object> delCart(HttpServletRequest request, HttpServletResponse response){
        Users user = (Users)request.getSession().getAttribute("user");
        int uid = user.getId();
        int pid = Integer.parseInt(request.getParameter("pid"));
        Map<String,Object> map = new HashMap<>();
        int count = cartService.delCart(uid, pid);
        if(count>0){
            map.put("flag",1);
        }else{
            map.put("flag",0);
        }
        return map;
    }
}
